﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMS
{
    public partial class Payment : Form
    {
        private int recordIdGlobal;
        private string p1;
        private string p2;
        private int p3;
        private int p4;
        private float firstPointGl;
        private float lastPointGl;
        private string registerPlaceGlobal;
        private float p5;
        string connectionString = ConfigurationManager.ConnectionStrings["OrlcConnect"].ConnectionString;
        public Payment()
        {
            InitializeComponent();
        }

        public Payment(int recordIdGlobal, string customerId, string customerName, int month, int year, float firstPointGl, float lastPointGl, string registerPlaceGlobal, float p5)
        {
            InitializeComponent();
            // TODO: Complete member initialization
            this.recordIdGlobal = recordIdGlobal;
            this.p1 = customerId;
            this.p2 = customerName;
            this.p3 = month;
            this.p4 = year;
            this.firstPointGl = firstPointGl;
            this.lastPointGl = lastPointGl;
            this.registerPlaceGlobal = registerPlaceGlobal;
            this.p5 = p5;
            txtCustomerId.Text = customerId;
            txtCustomerName.Text = customerName;
            txtMonth.Text = month.ToString();
            txtYear.Text = year.ToString();
            txtFirstPoint.Text = firstPointGl.ToString();
            txtLastpoint.Text = lastPointGl.ToString();
            txtRegisterPlace.Text = registerPlaceGlobal;
            txtPrice.Text = p5.ToString();
            txtTotal.Text = ((lastPointGl - firstPointGl) * p5).ToString();
            
        }

        private void btnPaid_Click(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Value.ToShortDateString();
            var connection = new OracleConnection(connectionString);
            try
            {
                connection.Open();

                OracleCommand cmd = connection.CreateCommand();
                OracleTransaction trans = connection.BeginTransaction();
                cmd.Transaction = trans;
                if (txtRegisterPlace.Text.Equals("Hai Chau"))
                {
                    cmd.CommandText = String.Format(@"INSERT INTO payment (datepaid, recordid) values ('{0}', {1})", date, recordIdGlobal);
                    MessageBox.Show("Paid successfully (HaiChau) !");
                }
                else
                {
                    cmd.CommandText = String.Format(@"INSERT INTO payment@dblink_elec (datepaid, recordid) values ('{0}', {1})", date, recordIdGlobal);
                    MessageBox.Show("Paid successfully (ThanhKhe) !");
                }
                cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

    }
}
